//
//  FirstViewController.m
//  CoreDataAddressBook
//
//  Created by dllo on 15/11/20.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "FirstViewController.h"
#import "WXCoreDataManager.h"
#import "Entity.h"
@interface FirstViewController ()
@property(nonatomic, strong)WXCoreDataManager *manager;

@property(nonatomic, retain)UILabel *nameLabel;
@property(nonatomic, retain)UILabel *sexLabel;
@property(nonatomic, retain)UILabel *numberLabel;
@property(nonatomic, retain)UILabel *telephotoLabel;
@property(nonatomic, retain)UILabel *qqLabel;
@property(nonatomic, retain)UILabel *addressLabel;

@property(nonatomic, retain)UITextField *nameTextField;
@property(nonatomic, retain)UITextField *sexTextField;
@property(nonatomic, retain)UITextField *numberTextField;
@property(nonatomic, retain)UITextField *telephotoTextField;
@property(nonatomic, retain)UITextField *qqTextField;
@property(nonatomic, retain)UITextField *addressTextField;

@property(nonatomic, retain)UIButton *addButton;
@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.manager = [WXCoreDataManager defaults];
    NSLog(@"url --- %@", [self.manager applicationDocumentsDirectory]);
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 120, 90, 50)];
    self.nameLabel.text = @"姓名:";
    self.nameLabel.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.nameLabel];
    self.nameLabel.textAlignment = 1;
    self.nameLabel.layer.borderWidth = 1;
    [_nameLabel release];
    
    self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(170, 120, 150, 50)];
    self.nameTextField.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.nameTextField];
    self.nameTextField.layer.borderWidth = 1;
    [_nameTextField release];
    
    self.sexLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 180, 90, 50)];
    self.sexLabel.text = @"性别:";
    self.sexLabel.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.sexLabel];
    self.sexLabel.textAlignment = 1;
    self.sexLabel.layer.borderWidth = 1;
    [_sexLabel release];
    
    self.sexTextField = [[UITextField alloc] initWithFrame:CGRectMake(170, 180, 150, 50)];
    self.sexTextField.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.sexTextField];
    self.sexTextField.layer.borderWidth = 1;
    [_sexTextField release];
    
    self.numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 240, 90, 50)];
    self.numberLabel.text = @"学号:";
    self.numberLabel.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.numberLabel];
    self.numberLabel.textAlignment = 1;
    self.numberLabel.layer.borderWidth = 1;
    [_numberLabel release];
    
    self.numberTextField = [[UITextField alloc] initWithFrame:CGRectMake(170, 240, 150, 50)];
    self.numberTextField.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.numberTextField];
    self.numberTextField.layer.borderWidth = 1;
    [_numberTextField release];
    
    self.telephotoLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 300, 90, 50)];
    self.telephotoLabel.text = @"电话:";
    self.telephotoLabel.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.telephotoLabel];
    self.telephotoLabel.textAlignment = 1;
    self.telephotoLabel.layer.borderWidth = 1;
    [_telephotoLabel release];
    
    self.telephotoTextField = [[UITextField alloc] initWithFrame:CGRectMake(170, 300, 150, 50)];
    self.telephotoTextField.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.telephotoTextField];
    self.telephotoTextField.layer.borderWidth = 1;
    [_telephotoTextField release];
    
    self.qqLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 360, 90, 50)];
    self.qqLabel.text = @"QQ号码:";
    self.qqLabel.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.qqLabel];
    self.qqLabel.textAlignment = 1;
    self.qqLabel.layer.borderWidth = 1;
    [_qqLabel release];
    
    self.qqTextField = [[UITextField alloc] initWithFrame:CGRectMake(170, 360, 150, 50)];
    self.qqTextField.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.qqTextField];
    self.qqTextField.layer.borderWidth = 1;
    [_qqTextField release];
    
    self.addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 420, 90, 50)];
    self.addressLabel.text = @"地址:";
    self.addressLabel.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.addressLabel];
    self.addressLabel.textAlignment = 1;
    self.addressLabel.layer.borderWidth = 1;
    [_addressLabel release];
    
    self.addressTextField = [[UITextField alloc] initWithFrame:CGRectMake(170, 420, 150, 50)];
    self.addressTextField.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.addressTextField];
    self.addressTextField.layer.borderWidth = 1;
    [_addressTextField release];

    self.addButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.addButton.frame = CGRectMake(150, 530, 60, 50);
    [self.view addSubview:self.addButton];
    [self.addButton setTitle:@"添加" forState:(UIControlStateNormal)];
    self.addButton.backgroundColor = [UIColor orangeColor];
    [self.addButton addTarget:self action:@selector(add:) forControlEvents:(UIControlEventTouchUpInside)];
    
}
- (void)add:(UIButton *)button{
    
    if ([self.nameTextField.text isEqualToString:@""] || [self.telephotoTextField.text isEqualToString:@""]) {
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"姓名和电话不能为空" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alter show];
        [alter release];
        NSLog(@"NO");
    } else{
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"AddressBook" inManagedObjectContext:self.manager.managedObjectContext];
        Entity *ss = [[Entity alloc] initWithEntity:entity insertIntoManagedObjectContext:self.manager.managedObjectContext];
        
        ss.name = self.nameTextField.text;
        ss.sex = self.sexTextField.text;
        ss.number = self.numberTextField.text;
        ss.telephone = self.telephotoTextField.text;
        ss.qq = self.qqTextField.text;
        ss.address = self.addressTextField.text;
        
        
        [self.manager saveContext];
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"添加成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alter show];
        [alter release];
        
        

    }
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
